﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Web;
using IronOne.SalesOne.DataObjects.QuotationReportDSTableAdapters;


namespace IronOne.SalesOne.SalesOneWeb.SalesQuotations
{

    public partial class ViewReportMNLife : System.Web.UI.Page
    {
    
        string lifeCode = string.Empty;
        string quoNum = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {           

            
                quoNum = Page.Request.Params["QuotationNum"].ToString();
                lifeCode = Page.Request.Params["Life"].ToString();


                ReportDocument RptCodes = new ReportDocument();
                CodesQuoTableAdapter CdAdp = new CodesQuoTableAdapter();

                DataTable dt1 = CdAdp.GetDataByCodes(lifeCode, quoNum);



                ReportDocument RptPrem = new ReportDocument();
                PremiumQuoTableAdapter PrAdp = new PremiumQuoTableAdapter();

                DataTable dt2 = PrAdp.GetDataByPremium(lifeCode, quoNum);



                ReportDocument rptNotice = new ReportDocument();
                NoticeQuoTableAdapter NtAdp = new NoticeQuoTableAdapter();

                DataTable dt3 = NtAdp.GetDataNotice(quoNum);




                ReportDocument MNReport = new ReportDocument();
                string reportPath = Server.MapPath("ReportMinLife.rpt");
                MNReport.Load(reportPath);

                SalesQuotationAdpTableAdapter SQAdpt = new SalesQuotationAdpTableAdapter();

                DataTable dt0 = SQAdpt.GetlSalesQuotation(lifeCode, quoNum);

                MNReport.OpenSubreport("SbReportCodes.rpt").SetDataSource((DataTable)dt1);

                MNReport.OpenSubreport("SbReportPremium.rpt").SetDataSource((DataTable)dt2);

                MNReport.OpenSubreport("SbReportNotice.rpt").SetDataSource((DataTable)dt3);

                MNReport.SetDataSource((DataTable)dt0);


                CrystalReportViewer2.ReportSource = MNReport;


                MemoryStream oStream; // using System.IO
                oStream = (MemoryStream)
                MNReport.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
                Response.Clear();
                Response.Buffer = true;
                Response.ContentType = "application/pdf";
                Response.BinaryWrite(oStream.ToArray());
                Response.End();
          
        }


            
        private string GetReportSQL()
        {
            StringBuilder sql = new StringBuilder();
            bool isAnd = false;

            sql.Append(" SELECT QuoDetails.BenefitCd, QuoDetails.TermQuoDetails.BenefitCd, QuoDetails.Term, QuoDetails.Pdpm, QuoDetails.SumAssured, QuoDetails.Life AS QuoDeLife, QuoDetails.Premium, ");
            sql.Append(" QuoDetails.QuotationNo AS QuoDeQuoNum, QuoDetails.SOCode AS QuoDeSOCode, ProspectQuotations.ProspectId, ");
            sql.Append(" ProspectQuotations.PlanCode AS ProsQuoPlanCode, ProspectQuotations.SOCode AS ProsQuoSOCode, ProspectQuotations.Initials, ProspectQuotations.Dob, ProspectQuotations.Surname, ProspectQuotations.Title, ProspectQuotations.QuotationNo AS ProsQuoQuoNum,  ");
            sql.Append(" QuotationSummary.Code AS QuoSumCode, QuotationSummary.SoCode AS QuoSumSOCode, QuotationSummary.Cover, QuotationSummary.Life AS QuoSumLife, QuotationSummary.QuotationNo AS QuoSumQuoNum, OfficeDetails.Address1, OfficeDetails.Address2, ");
            sql.Append(" OfficeDetails.Address3, OfficeDetails.Phone, QuoBonus.QuotationNo AS QuoBonQuoNum, QuoBonus.PrintCode AS QuoBoPCode, QuoBonus.Bonus, PlanCodeTitles.Title AS PCodeTitTitle, PlanCodeTitles.Sequence, PlanCodeTitles.SoCode AS PCodeTitSOCode ");
            sql.Append(" FROM QuoDetails INNER JOIN QuotationSummary ON QuoDetails.Life = QuotationSummary.Life INNER JOIN ProspectQuotations ON QuoDetails.QuotationNo = ");
            sql.Append(" ProspectQuotations.QuotationNo INNER JOIN PlanCodeTitles ON QuotationSummary.Code = PlanCodeTitles.Code INNER JOIN QuoBonus ON QuoDetails.QuotationNo = QuoBonus.QuotationNo CROSS JOIN OfficeDetails ");
            sql.Append(" WHERE ");


            if (!String.IsNullOrEmpty(lifeCode))
            {
                sql.Append(" QuoDeLife = " + lifeCode);
                isAnd = true;
            }

            if (isAnd)
            {
                sql.Append(" AND ");
                sql.Append(" ProsQuoQuoNum = " + quoNum);
            }

            return sql.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/SalesQuotations/SearchSalesQuatation.aspx");

            

        }






            


        }
    }

